package Utils;

import java.io.File;
import java.io.IOException;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.event.ConfigurationEvent;
import org.apache.commons.configuration.event.ConfigurationListener;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConfigurationService extends PropertiesConfiguration implements ConfigurationListener {
	private static Log logger = LogFactory.getLog(ConfigurationService.class);
    private static final String PROPS_RESOURCE = "prefs.properties";
    private static ConfigurationService instance = null;
    private String propsFile = PROPS_RESOURCE;
	
    public static final ConfigurationService getInstance() {
        if (instance == null) {
            instance = new ConfigurationService();
            instance.loadProperties();
        }
        return instance;
    }
    
    private void loadProperties() {
        try {
            File is = null;
            // Class c = getClass();
            is = new File(propsFile);
            if (is != null) {
                loadProperties(is);
            }
        } catch (IOException ioe) {
            logger.warn("Could not load engine properties", ioe);
        } catch (Exception ex) {
        	logger.warn("Error", ex);
        }
    }
    
    private void loadProperties(File is)
            throws IOException  {
        try {
            instance.setFile(is);
            instance.setReloadingStrategy(new FileChangedReloadingStrategy());
            instance.setAutoSave(true);
            instance.load();
            logger.info("Loaded engine properties from " + propsFile);
        } catch (Exception e) {
            throw new IOException("Failed to load properties from " + propsFile);
        }
    }
    
    public void load() {
    	
    }
    
	@Override
	public void configurationChanged(ConfigurationEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("something change in prefs.properties file, you need to do something!");
	}
}
